package com.ssm.controller;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.ssm.pojo.Book;
import com.ssm.pojo.Yong;
import com.ssm.pojo.Record;
import com.ssm.pojo.Type;
import com.ssm.pojo.Page;
import com.ssm.service.BookService;
@Controller
@RequestMapping("/yong")
public class YongController {
     @Autowired
    private BookService bookService;

    @RequestMapping("/index")
    public String toIndex() {
        return "/yongs/index";
    }

    @RequestMapping("/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/index";
    }

    @RequestMapping("/show")
    //Long.toString(book.getTypeid()) != null(把long类型转成String)
    public String toShow(Model model, Book book) {
        System.out.println(book);
        if (Long.toString(book.getTypeid()) != null) {
            model.addAttribute("typeid", book.getTypeid());
        }
        if (!"".equals(book.getName()) || book.getName().length() > 0) {
            model.addAttribute("name", book.getName());
        }
        Page page = bookService.getAllBooks(book);
        model.addAttribute("page", page);
        model.addAttribute("currentPage", book.getCurrentPage());
        model.addAttribute("totalPage", page.getTotalPage());
        return "/yongs/show";
    }

    @RequestMapping("/record")
    public String toBorrow(Model model, long id) {
        Book book = bookService.getBook(id);
        System.out.println(book);
        model.addAttribute("book", book);
        return "/yongs/records";
    }

    /**
     * 差用户id未注入，到时候添加登录功能在补充
     */
    @RequestMapping("/insert")
    public String insertRecord(Record record,HttpSession session){
        Yong yong = (Yong) session.getAttribute("yong");
        System.out.println("id"+record);
        record.setYongid(yong.getId());
        Date date = new Date();
        record.setRent(date);
        record.setStatus(0);
        bookService.insertRecord(record);
        return "redirect:/yong/back";
    }

    /**
     * @return 也是注入用户ID，这里先用1
     */
    @RequestMapping("/back")
    public String getRecord(Model model, HttpSession session) {
        Yong yong =(Yong) session.getAttribute("yong");
        List<Record> records = bookService.getRecord(yong.getId());
        model.addAttribute("record",records);
        return "yongs/back";
    }

    /**
     * @return 这里注入session再把1换成session取得的id
     */
    @RequestMapping("/showBackBook")
    public String showBackBook(Model model, HttpSession session) {
        Yong yong =(Yong) session.getAttribute("yong");
        List<Record> backs = bookService.getBacks(yong.getId());
        model.addAttribute("backs", backs);
        return "yongs/showBackBook";
    }

    @RequestMapping("/backBooks")
    public String backBooks(long id) {
        bookService.updateBacks(id);
        return "redirect:/yong/showBackBook";
    }


}
